<%--
  Created by IntelliJ IDEA.
  User: d's
  Date: 2019/10/21
  Time: 13:12
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>

<head>
  <title>Title</title>
  <link rel="stylesheet" href="/layui/css/layui.css">
  <script src="/js/jquery-3.4.1.min.js"></script>
  <script src="/layui/layui.all.js"></script>
  <script src="/layer/layer.js"></script>
</head>
<style>
#div-nav{
    width: 50%;
}
</style>

<body>

<input type="text" hidden="hidden" value="${supper}" id="quanxian">

<button id="weibo">微博管理</button><br><br>
<button id="zhanghu">账户管理</button><br><br>
    <div id="div-nav">
    <div id="div-table">
      <table class="layui-hide" id="test" lay-filter="demo"></table>
    </div></div>
    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>

<br><br>
<input type="button" value="添加数据" id="add">
</body>
<script>
  $(function () {
        layui.use('table', function (data) {
            var table = layui.table;
            table.render({
                elem: '#test'
                , url: "/user/getuserlist.do"
                , page: true //开启分页
                , width: 1200
                , limit: 5
                , limits: [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]
                , width: 800
                , cols: [[
                    { field: 'id', align:"center", title: '序号' }
                    , { field: 'picpath', title: '用户', align:"center", templet: function (data) {
                            return '<img src="' + data.picpath + '" width:30px; height:30px;>';
                        }
                     }
                    , {
                        field: 'nickname', title: '昵称',  align:"center"
                    }
                    , { field: 'mobile', title: '手机',  align:"center" },
                    , { field: 'address', title: '地址',width:72,align:"center" },
                    { fixed: 'right', align: 'center', toolbar: '#barDemo' }
                ]]
                , id: 'userTableReload',
                done: function (res, curr, count) {// 表格渲染完成之后的回调
                    var that = this.elem.next();
                    res.data.forEach(function (item, index) {
                        //console.log(item.empName);item表示每列显示的数据
                        if (index % 2 == 0) {
                            var tr = that.find(".layui-table-box tbody tr[data-index='" + index + "']").css("background-color", "turquoise");
                        } else {
                            var tr = that.find(".layui-table-box tbody tr[data-index='" + index + "']").css("background-color", "white");
                        }
                    });
                }
            });
            var $ = layui.$, active = {
                reload: function () {
                    var type = $("#type")
                    var search = $("#search")
                    table.reload('userTableReload', {
                        where: {
                            type: type.val(),
                            search: search.val()
                        }
                    });
                }
            };
            $('.demoTable .layui-btn').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
            table.on('tool(demo)', function (obj) {
                var data = obj.data;
                if (obj.event === 'del') {
                    $.ajax({
                        type: 'post',
                        url: "",
                        async: true,
                        data: {
                            id: data.id
                        },
                        dataType: 'json',
                        success: function (rs) {
                            if ("success" == rs.state) {
                                layer.msg('删除成功')
                            } else if ("failed" == rs.state) {
                                layer.msg("删除失败")
                            }
                        }
                    });
                    layer.confirm('真的删除行么', function (index) {
                        obj.del();
                        layer.close(index);
                    });
                }
            });

        });
        function logout() {
            layer.msg('确定退出系统吗', {
                time: 20000,
                btn: ['确定', '取消']
                , yes: function () {
                    sessionStorage.clear()
                    location.replace('/login.html')
                }
            });

        }
    })

  $('#weibo').click(function () {
      window.location.href="http://localhost:8080/jsp/blogList.jsp";
  })

  $('#zhanghu').click(function () {
      var quan = $('#quanxian').val();
      if(quan==2){
          window.location.href="http://localhost:8080/jsp/userList.jsp";
      }else{
          window.location.href="http://localhost:8080/jsp/updateUser.jsp";
      }
  })
    
    $('#add').click(function () {
        window.location.href="http://localhost:8080/jsp/insert.jsp";
    })

</script>
</html>